package com.manager.common.job;

import com.manager.common.tcp.client.TcpClient;
import com.manager.common.tcp.constant.TcpDataType;
import com.manager.common.tcp.decode.impl.BusinessDecode;
import com.manager.common.utils.RemoteExecute;
import net.sf.json.JSONObject;

public class ReceiptThread extends Thread {

    private String receiptUrl;

    private String tcpFileReportUrl;

    private String exceptionContainerUrl;

    public ReceiptThread(String receiptUrl,String tcpFileReportUrl,String exceptionContainerUrl) {
        this.receiptUrl = receiptUrl;
        this.tcpFileReportUrl = tcpFileReportUrl;
        this.exceptionContainerUrl = exceptionContainerUrl;
    }

    @Override
    public void run() {
        while (true) {
            try{
                BusinessDecode businessDecode = TcpClient.getInstance().getReceiptQueue().take();
                if(businessDecode.getTcpDataType().equals(TcpDataType.RECEIPT)){
                    JSONObject jsonObject = JSONObject.fromObject(businessDecode.getContent());
                    RemoteExecute.execute(receiptUrl,jsonObject.toString());
                }else if (businessDecode.getTcpDataType().equals(TcpDataType.CONTAINER_MANAGER)){
                    JSONObject jsonObject = JSONObject.fromObject(businessDecode.getContent());
                    RemoteExecute.execute(exceptionContainerUrl,jsonObject.toString());
                }else if(businessDecode.getTcpDataType().equals(TcpDataType.TCP_FILE_REPORT)){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("content",businessDecode.getContent());
                    jsonObject.put("code",businessDecode.getTcpDataType().getCode());
                    RemoteExecute.execute(tcpFileReportUrl,jsonObject.toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
